import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:p_location/alarm/constant/alarm_http_url.dart';
import 'package:p_location/alarm/res/alarm_vehicle_item.dart';
import 'package:p_location/alarm/res/vehicle/obj_alarm_vehicle.dart';
import 'package:p_location/alarm/res/vehicle/res_alarm_vehicle_detail.dart';
import 'package:p_location/alarm/utils/trace_judger.dart';
import 'package:p_location/common/res/common_res.dart';
import 'package:p_location/common/widget/no_result_widget.dart';
import 'package:p_location/common/widget/tag_view.dart';
import 'package:p_location/common/widget/vertical_info_item.dart';
import 'package:p_location/map/res/search_person_detail.dart';
import 'package:p_location/net/dio_http_client.dart';
import 'package:p_location/net/net_constants.dart';
import 'package:p_location/net/net_listener_impl.dart';
import 'package:p_location/utils/navigator_util.dart';
import 'package:p_location/utils/net/search_person_util.dart';
import 'package:p_location/utils/string_util.dart';

class PageAlarmVehicleDetail extends StatefulWidget {
  final AlarmVehicleItem item;

  final int id;

  //final String alarmType;

  final String alarmTypeName;

  final String info;

  final String time;

  final String status;

  PageAlarmVehicleDetail(this.item, this.id, this.alarmTypeName, this.info,
      this.time, this.status);

  @override
  _PageAlarmVehicleDetailState createState() => _PageAlarmVehicleDetailState();
}

class _PageAlarmVehicleDetailState extends State<PageAlarmVehicleDetail> {
  String errMsg = "";
  bool isLoading = true;
  ObjAlarmVehicle oneKeyDetail;

  SearchPersonDetail fullDetail;

  @override
  void initState() {
    super.initState();
    if (widget.item != null && StringUtil.isNotEmpty(widget.item.vehicleSn)) {
      _getFullInfoBySn(widget.item.vehicleSn);
    } else {
      _getDetail();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: IconButton(
              tooltip: '返回',
              icon: Icon(Icons.arrow_back_ios, color: Colors.white),
              onPressed: () {
                Navigator.of(context).pop();
              }),
          title: Text("报警详情"),
          elevation: 0,
          brightness: Brightness.dark,
          centerTitle: true,
        ),
        body: isLoading
            ? _loadingView
            : StringUtil.isEmpty(errMsg)
                ? _contentView
                : _exceptionView);
  }

  get _loadingView => Center(child: CircularProgressIndicator());

  get _contentView => ListView(
        children: [
          VerticalInfoItem("事件", widget.info),
          VerticalInfoItem("报警类型", widget.alarmTypeName),
          VerticalInfoItem("报警时间", widget.time),
          IntrinsicHeight(
            child: Row(
              mainAxisSize: MainAxisSize.max,
              children: [
                Expanded(
                  child: VerticalInfoItem("报警状态", widget.status),
                  flex: 2,
                ),
                Expanded(
                  child: Container(
                      decoration: BoxDecoration(
                          color: Colors.white,
                          border: Border(
                              bottom:
                                  BorderSide(color: Colors.grey, width: 0.4))),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Offstage(
                            offstage: widget.status != '报警中' || TraceJudger.isHide(widget.alarmTypeName),
                            child: TagView(
                              "追踪",
                              paddingLeft: 15,
                              paddingRight: 15,
                              paddingTop: 1,
                              paddingBottom: 1,
                              radius: 5,
                              strokeColor: Color.fromARGB(255, 56, 160, 253),
                              textColor: Color.fromARGB(255, 56, 160, 253),
                              fillColor: Colors.white,
                              onPress: () {
                                if(fullDetail == null){
                                  fullDetail = SearchPersonDetail();
                                  fullDetail.id = widget.item.id;
                                  fullDetail.name = widget.item.carNumber;
                                  fullDetail.sn = widget.item.vehicleSn;
                                  fullDetail.typeName = widget.item.carType;
                                }
                                NavigatorUtil.jumpToLocationPageWithTarget(
                                    context, fullDetail);
                              },
                            ),
                          )
                        ],
                      )),
                ),
              ],
            ),
          ),
          VerticalInfoItem("驾驶员", oneKeyDetail.driverName),
          VerticalInfoItem("电子运单号", oneKeyDetail.waybillNumber),
          VerticalInfoItem("所属单位", oneKeyDetail.driverDepartName)
        ],
      );

  get _exceptionView => NoResultWidget('images/img_no_accident.png', errMsg);

  _getDetail() async {
    Map<String, dynamic> params = {};
    params["id"] = widget.id;
    await DioHttpClient.requestPost(context, AlarmHttpUrl.ALARM_VEHICLE_URL,
        params, NetListenerImpl(onSuccess, onError),
        contentType: NetConstants.POST_TYPE_FORM);
  }

  void onSuccess(Response response) {
    setState(() {
      if (response != null && response.data != null) {
        ResAlarmVehicleDetail res =
            ResAlarmVehicleDetail.fromJson(response.data);
        if (res != null && res.data != null) {
          oneKeyDetail = res.data;
        }
        isLoading = false;
      }
    });
  }

  void onError(CommonRes errorRes) {
    setState(() {
      errMsg = errorRes.msg ?? "请求异常";
      isLoading = false;
    });
  }

  _getFullInfoBySn(String keyword) {
    SearchPersonUtil searchPersonUtil =
    SearchPersonUtil(keyword, (List<SearchPersonDetail> _detailList) {
      if (_detailList != null && _detailList.length > 0) {
        SearchPersonDetail searchPersonDetail = _detailList[0];
        if (searchPersonDetail != null) {
          fullDetail = searchPersonDetail;
        }
      }
      _getDetail();
    }, () {
      _getDetail();
    });
    searchPersonUtil.requestSearchPerson(context);
  }
}
